<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>租赁管理</el-breadcrumb-item>
                <el-breadcrumb-item>所有车辆</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="roleFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="roleFormData.carName" placeholder="请输入车名"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-input v-model="roleFormData.carPlate" placeholder="请输车牌号"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary"  @click="initCatTable">查询</el-button>
                    <el-button type="primary" @click="addCar">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="#"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="carName"
                            label="汽车名称"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="typeName"
                            label="车辆类型">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="车辆类型Id"
                            v-if="show">
                    </el-table-column>
                    <el-table-column
                            prop="configName"
                            label="车辆配置">
                    </el-table-column>
                    <el-table-column
                            prop="configId"
                            label="车辆配置Id"
                            v-if="show">
                    </el-table-column>
                    <el-table-column
                            prop="carPlate"
                            label="车牌号"
                    >
                    </el-table-column>
                    <el-table-column
                                prop="brandName"
                                label="品牌名"
                        >
                    </el-table-column>
                    <el-table-column
                            prop="brandId"
                            label="品牌id"
                            v-if="show">
                    </el-table-column>
                    <el-table-column
                            prop="vin"
                            label="车辆识别码">
                    </el-table-column>
                    <el-table-column
                            prop="intro"
                            label="车辆简介">
                    </el-table-column>
                    <el-table-column
                            prop="speedBox"
                            label="变速箱">
                    </el-table-column>
                    <el-table-column
                            prop="airSuction"
                            label="进气方式">
                    </el-table-column>
                    <el-table-column
                            prop="seating"
                            label="座位数">
                    </el-table-column>
                    <el-table-column
                            prop="displacement"
                            label="排量">
                    </el-table-column>
                    <el-table-column
                            prop="tankCapacity"
                            label="油箱容量">
                    </el-table-column>
                    <el-table-column
                            prop="fcphk"
                            label="百公里油耗">
                    </el-table-column>
                    <el-table-column
                            prop="buyPrice"
                            label="进价">
                    </el-table-column>
                    <el-table-column
                            prop="price"
                            label="租价">
                    </el-table-column>
                    <el-table-column
                            prop="state"
                            label="状态">
                        <template slot-scope="scope">
                            <p v-if="tableData[scope.$index].state==1">可租</p>
                            <p v-if="tableData[scope.$index].state==2">维护</p>
                            <p v-if="tableData[scope.$index].state==3">租赁中</p>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="shopId"
                            label="店铺id"
                            v-if="show">
                    </el-table-column>
                    <el-table-column
                            prop="shopName"
                            label="店面名">
                    </el-table-column>

                    <el-table-column label="操作" width="266  px" fixed="right">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="updaCar(scope.row,'form')" circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                            <el-button type="warning" round  @click="Maintain(scope.row.id)">汽车维修管理</el-button>
                        </template>
                    </el-table-column >
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
            <!--汽车信息-->
            <el-dialog title="汽车信息" :visible.sync="dialogFormVisible" width="33%" style="text-align: center">
                <el-form :model="form" ref="form"  :rules="rules">
                    <el-form-item label="汽车Id" v-show="false">
                        <el-input v-model="form.id" autocomplete="off" style="width: 75%"></el-input>
                    </el-form-item>
                    <el-form-item label="汽车名称" prop="carName">
                        <el-input v-model="form.carName" autocomplete="off" style="width: 75%"></el-input>
                    </el-form-item>
                    <el-form-item label="汽车类型" prop="type">
                        <el-select v-model="form.type" autocomplete="off"  placeholder="请选择活动区域" style="width: 75%">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.typeName"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="汽车配置" prop="configId">
                        <el-select v-model="form.configId" autocomplete="off" placeholder="请选择活动区域" style="width: 75%" >
                            <el-option
                                    v-for="item in optionsConfig"
                                    :key="item.value"
                                    :label="item.configName"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="车牌号" prop="carPlate">
                        <el-input v-model="form.carPlate" autocomplete="off" style="width: 75%" ></el-input>
                    </el-form-item>
                    <el-form-item label="车辆简介">
                        <el-input
                                v-model="form.intro"
                                autocomplete="off"
                                style="width: 75%"
                                type="textarea"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="品牌" prop="brandId">
                        <el-select v-model="form.brandId" autocomplete="off" placeholder="请选择活动区域" style="width: 75%">
                            <el-option
                                    v-for="item in optionsbrand"
                                    :key="item.value"
                                    :label="item.brandName"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="进价" prop="buyPrice">
                        <el-input v-model="form.buyPrice" autocomplete="off" style="width: 75%" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="租价" prop="price">
                        <el-input v-model="form.price" autocomplete="off" style="width: 75%" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="汽车状态" prop="state">
                        <el-select v-model="form.state" autocomplete="off" placeholder="请选择活动区域" style="width: 75%">
                            <el-option label="可租" :value="1"></el-option>
                            <el-option label="租赁中" :value="3"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="门店" prop="shopId">
                        <el-select v-model="form.shopId" autocomplete="off" placeholder="请选择活动区域" style="width: 75%">
                            <el-option
                                    v-for="item in optionsShop"
                                    :key="item.value"
                                    :label="item.shopName"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addOrupda">确 定</el-button>
                </div>
            </el-dialog>
            <!--添加维修维修记录-->
            <el-dialog
                    title="添加维修维修记录"
                    :visible.sync="editmtRecordDialog"
                    width="30%">
                <!--            model表单数据-->
                <el-form  label-width="80px" :model="mtRecordForm" :rules="mtRecordFormRules" ref="mtRecordFormRef">
                    <el-form-item label="汽车id" prop="carId">
                        <el-input v-model="mtRecordForm.carId" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="维修厂" prop="maintainShopId">
                        <el-select v-model="mtRecordForm.maintainShopId" placeholder="请选择维修厂" >
                            <el-option
                                    v-for="itemsss in shopArray"
                                    :key="itemsss.id"
                                    :label="itemsss.name"
                                    :value="itemsss.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="维修时间" prop="addTime">
                        <el-date-picker
                                v-model="mtRecordForm.addTime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime"
                                placeholder="选择日期时间"
                                align="right"
                                :picker-options="pickerOptions">
                        </el-date-picker>

                    </el-form-item>
                    <!--<el-form-item label="维修金额" prop="amount">-->
                        <!--<el-input v-model="mtRecordForm.amount"></el-input>-->
                    <!--</el-form-item>-->
                    <el-form-item label="备注" >
                        <el-input v-model="mtRecordForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="editmtRecordDialog = false">取 消</el-button>
                        <el-button type="primary" @click="addMM">确 定</el-button>
                    </span>
            </el-dialog>
            <el-dialog
                    title="汽车维修中"
                    :visible.sync="Carrepairing"
                    width="30%">
                <el-form  label-width="80px" :model="mtRecordFormrepairing" :rules="mtRecordrepairing" ref="mtRecordFormrepairing">
                    <el-form-item label="汽车id" >
                        <el-input v-model="mtRecordFormrepairing.carId" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="id" prop="Id" v-if="show">
                        <el-input v-model="mtRecordFormrepairing.id" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="维修金额" prop="amount">
                        <el-input v-model="mtRecordFormrepairing.amount"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="Carrepairing = false">取 消</el-button>
                        <el-button type="primary" @click="updateMM">车辆维修完成</el-button>
                    </span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "AllCat",
    computed:{
        rules(){
            var checkPlateNum = (rule, value, callback) => {
                if (value && value !== '') {
                    let regEn = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$)/
                    if (!regEn.test(value)) {
                        callback(new Error('请输入正确车牌号'))
                    } else {
                        callback()
                    }
                } else {
                    callback()
                }
            };
            return{
                carPlate:[
                    { required: true, message: '请输入车牌号', trigger: "blur" },
                    { validator: checkPlateNum, trigger: "blur" }
                ],
                carName: [{ required: true, message: '请输入汽车名称', trigger: "blur" }]
                ,type:[{ required: true, message: '请选择汽车类型', trigger: "change" }]
                ,configId:[{ required: true, message: '请选择汽车配置', trigger: "change" }]
                ,brandId:[{ required: true, message: '请选择汽车品牌', trigger: "change" }]
                ,state:[{ required: true, message: '请选择汽车状态', trigger: "change" }]
                ,shopId:[{ required: true, message: '请选择租车门店', trigger: "change" }]
                ,buyPrice:[{ required: true, message: '请输入进车价', trigger: "blur" }]
                ,price:[{ required: true, message: '请输入租车价', trigger: "blur" }]

            }
        }
    },
        data(){
            return {
                roleFormData:{},
                tableData:[],
                currentPage:1,
                pageSize:5,
                total:0,
                form:{
                    id:'',
                    carName:'',
                    type:'',
                    configId:'',
                    carPlate:'',
                    brandId:'',
                    buyPrice:'',
                    price:'',
                    state:''
                },
                dialogFormVisible:false,
                labelPosition:'left',
                options: [],
                optionsConfig: [],
                optionsbrand: [],
                optionsShop:[],
                show:false,
                mtRecordForm:{},
                mtRecordFormrepairing:{},
                editmtRecordDialog:false,
                Carrepairing:false,
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    },{
                        text: '一年前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 365);
                            picker.$emit('pick', date);
                        }
                    }],
                    disabledDate(time)
                    {
                        //return time.getTime() > Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的
                        return time.getTime() > Date.now();//可以选择当前时间

                    }
                },
                mtRecordFormRules:{
                    carId:[{required:true,message:"汽车不可为空",trigger:'blur'}],
                    maintainShopId:[{required:true,message:"必须选择维修厂",trigger:'blur'}],
                    addTime:[{required:true,message:"必须选择时间",trigger:'blur'}],
                },
                mtRecordrepairing:{
                    amount:[{ required: true, message: '请输入金额',trigger: 'blur'}]
                }
                ,
                shopArray:[],
                show:false
            }
        },
        created() {
            this.initCatTable();
            this.getCartype();
            this.getCarConfig();
            this.getCarBrand();
            this.getShop();
            this.selectListAll();
        },
        methods:{
            initCatTable(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/lease/tbCar/getAllCat/${this.currentPage}/${this.pageSize}`,this.roleFormData).then(function(resp){
                    that.tableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                });
            },
            del(id){
                var that=this;
                var param={id:id};
                this.$http.delete("/lease/tbCar/delCatById",{params:param}).then(function (r) {
                    that.$message(r.data.message);
                    that.initCatTable();
                    }
                )
            }
            ,getCartype(){
                var that=this;
                this.$http.get("lease/type/getAllType").then(r=>{
                        that.options=r.data.result.type;
                })
            },
            getCarConfig(){
               var that=this;
               this.$http.get("lease/config/getAllConfig").then(r=>{
                        that.optionsConfig=r.data.result.carConfigs;
               })
            },
            getCarBrand(){
                var that=this;
                this.$http.get("lease/brand/getCarBrand").then(r=>{
                    that.optionsbrand=r.data.result.CarBrand;
                })
            },
            getShop(){
                var that=this
                this.$http.get("lease/shop/getAllShop").then(r=>{
                    that.optionsShop=r.data.result.shop;
                })
            }
             ,
            addCar(form){
                this.dialogFormVisible = true;
                this.form={};
                this.$refs.form.resetFields();
            },
            updaCar(data,form){
                this.dialogFormVisible = true;
                // this.$nextTick(()=>{
                //     this.$refs.form.resetFields();
                // });
                var js= JSON.stringify(data);
                var da= JSON.parse(js);
                this.form = da;
                this.$refs.form.resetFields();

            },
            addOrupda(){
                var that=this;
                this.$refs.form.validate((valid)=>{
                    if (valid){
                        if (that.form.id){
                            this.$http.put("lease/tbCar/updateCar",this.form).then(r=>{
                                that.$message(r.data.message);
                                that.initCatTable();
                            })
                        } else if (!that.form.id){
                            this.$http.post("lease/tbCar/addCar",this.form).then(r=>{
                                that.$message(r.data.message);
                                that.initCatTable();
                            })
                        }
                        this.dialogFormVisible = false;
                    }

                })

            },
            Maintain(id){
                console.log(id)
                var that=this;
                this.$http.get(`/maintain/record/getCarById/${id}`).then(r=>{
                    console.log(r.data.result.car);
                    if (r.data.result.car==null){
                        that.editmtRecordDialog=true;
                        that.mtRecordForm.carId=id;
                    } else {
                        that.Carrepairing=true;
                        that.mtRecordFormrepairing=r.data.result.car
                    }
                })
            }
            ,selectListAll(){
                var that=this;
                this.$http.get("/maintain/shop/selectListAll").then(function (r) {
                    that.shopArray=r.data.result.mtShops;
                    // console.log(that.shopArray)
                })

            },
            addMM(){
                var that=this;
                var username=sessionStorage.getItem("username");
                this.$refs.mtRecordFormRef.validate((valid)=>{
                    if (valid){
                        this.$http.post(`/lease/tbCar/updateState2/${this.mtRecordForm.carId}`).then(r=>{});
                        this.$http.post(`/maintain/record/insertMtRecord/${username}`,this.mtRecordForm).then(function (r) {
                            // that.$message.success(r.data.message);
                            //提交后关闭弹窗
                            that.editmtRecordDialog=false;
                            //提交后刷新
                            that.initCatTable();
                            //消息通知
                            that.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                        })
                        that.mtRecordForm={};
                    }
                })
            },
            updateMM(){
                var that=this;
                this.$refs.mtRecordFormrepairing.validate((valid)=>{
                    if (valid){
                        this.$http.post(`/lease/tbCar/updateState1/${this.mtRecordFormrepairing.carId}`).then(r=>{});
                        this.$http.put(`/maintain/record/updateMtRecordById`, this.mtRecordFormrepairing).then(function (r) {
                            // that.$message.success(r.data.message);
                            //消息通知
                            that.$message.success("维修完成");
                            //提交后关闭弹窗
                            that.Carrepairing = false;
                            //提交后刷新
                            that.initCatTable();

                        })
                    }
                })
            }
            ,
            handleSizeChange(v){
                this.pageSize=v;
                this.initCatTable();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.initCatTable();
            }
        }
    }
</script>

<style scoped>

</style>
